package com.appkubes.base.core.mbp;

import com.appkubes.base.core.mbp.impl.AbstractBaseServiceImpl;
import com.appkubes.common.Response;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.io.Serializable;

public class AbstractBaseController<S extends AbstractBaseService<D, T>, D, T>
        extends AbstractBaseServiceImpl
        implements AbstractBaseService {

    @Autowired
    protected S baseService;

    /**
     * @return Response
     * @explain 查询对象  <swagger GET请求>
     * @author Rongjie
     * @time 2020年02月08日
     */
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "获取对象", notes = "作者：Rongjie")
    @ApiImplicitParam(paramType = "path", name = "id", value = "对象id", required = true, dataType = "String")
    public Response getUserById(@PathVariable("id") String id) {
        T obj = baseService.getById(id);
        if (null != obj) {
            return Response.success(obj);
        }
        return Response.error("未找到数据");
    }

    /**
     * @return Response
     * @explain 删除对象
     * @author Rongjie
     * @time 2020年02月08日
     */
    @PostMapping("/deleteById")
    @ApiOperation(value = "删除", notes = "作者：Rongjie")
    @ApiImplicitParam(paramType = "query", name = "id", value = "对象id", required = true, dataType = "String")
    public Response deleteById(String id) {
        if (checkDelByIdCondition(id)) {
            Boolean flag = baseService.removeById(id);
            if (flag) {
                return Response.success("删除成功！");
            }
        }
        return Response.error("删除失败！");
    }

    public Boolean checkDelByIdCondition(Serializable id) {
        return true;
    }
}

